package com.tg.app.view.grid;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tg.appcommon.android.TGLog;


/**
 * Created by HanHailong on 2017/9/24.
 */

public class BasePageIndicator extends View implements PageIndicator {

    private static final int DEFAULT_COLUMN = 1;

    protected RecyclerView mRecyclerView;
    private OnPageChangeListener mListener;
    private int mScrollState;
    private int mPageColumn = DEFAULT_COLUMN;
    protected int mCurrentPage;

    public BasePageIndicator(Context context) {
        super(context);
    }

    public BasePageIndicator(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BasePageIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private int curState = RecyclerView.SCROLL_STATE_IDLE;

    private RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            curState = newState;
            onPageScrollStateChanged(newState);

            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

            int position = 0, position1;
            if (layoutManager instanceof GridLayoutManager) {
                GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
                position = gridLayoutManager.findFirstVisibleItemPosition();
                int row = gridLayoutManager.getSpanCount();
                position = position / (row * mPageColumn);
            } else if (layoutManager instanceof LinearLayoutManager) {
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
                position = linearLayoutManager.findFirstVisibleItemPosition();
                position1 = linearLayoutManager.findLastVisibleItemPosition();

                TGLog.d("DeviceListBaseFragment", "onScrollStateChanged position = " + position + "position1 = " + position);
            }

            onPageSelected(position);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

            int position = 0;
            if (layoutManager instanceof GridLayoutManager) {
                GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
                position = gridLayoutManager.findFirstVisibleItemPosition();
                int row = gridLayoutManager.getSpanCount();
                position = position / (row * mPageColumn);
            } else if (layoutManager instanceof LinearLayoutManager) {
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
                position = linearLayoutManager.findFirstVisibleItemPosition();
            }

            if (mListener != null) {
                mListener.onPageScrolled(position,0,0);
            }
            TGLog.d("DeviceListBaseFragment", "onScrolled onPageSelected = " + position + "dx = " + dx + ", dy = " + dy);
            if (curState == RecyclerView.SCROLL_STATE_IDLE){
                if (dx > 0){
                    onPageSelected(position + 1);
                }else if (dx < 0){
                    onPageSelected(position -1);
                }

            }


        }
    };

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        TGLog.d("DeviceListBaseFragment", "onPageScrolled onPageSelected = " + position);

    }

    @Override
    public void onPageSelected(int position) {
//        if (mScrollState == RecyclerView.SCROLL_STATE_IDLE) {
        if (mCurrentPage == position) return;

        mCurrentPage = position;
        invalidate();
//        }

        if (mListener != null) {
            mListener.onPageSelected(position);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        mScrollState = state;

        if (mListener != null) {
            mListener.onPageScrollStateChanged(state);
        }
    }

    @Override
    public void setRecyclerView(RecyclerView view) {
        if (mRecyclerView == view) {
            return;
        }
        if (mRecyclerView != null) {
            mRecyclerView.removeOnScrollListener(mScrollListener);
        }
        if (view.getAdapter() == null) {
            throw new IllegalStateException("RecyclerView does not have adapter instance.");
        }
        mRecyclerView = view;
        mRecyclerView.addOnScrollListener(mScrollListener);
        invalidate();
    }

    @Override
    public void setRecyclerView(RecyclerView view, int initialPosition) {
        setRecyclerView(view);
        setCurrentItem(initialPosition);
    }

    @Override
    public void setCurrentItem(int item) {
        if (mRecyclerView == null) {
            throw new IllegalStateException("RecyclerView has not been bound.");
        }

        int currentPage = eachPageItemCount() * item;
        mCurrentPage = item;
        ((LinearLayoutManager)mRecyclerView.getLayoutManager()).scrollToPositionWithOffset(currentPage,0);
//        mRecyclerView.smoothScrollToPosition(currentPage);
        
        invalidate();
        if (mListener != null) {
            mListener.onPageSelected(mCurrentPage);
        }
    }

    @Override
    public void setOnPageChangeListener(OnPageChangeListener listener) {
        this.mListener = listener;
    }

    @Override
    public void notifyDataSetChanged() {
        invalidate();
    }

    @Override
    public void setPageColumn(int column) {
        if (column <= 0)
            throw new IllegalArgumentException("column must be not null");
        this.mPageColumn = column;
    }

    /**
     * The number of each page
     *
     * @return
     */
    protected int eachPageItemCount() {
        if (mRecyclerView == null) {
            return 0;
        }

        int row = 1;
        RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();

        if (layoutManager != null) {
            if (layoutManager instanceof GridLayoutManager) {
                GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
                row = gridLayoutManager.getSpanCount();
            }
        }

        return row * mPageColumn;
    }

    public int pageCount() {
        if (mRecyclerView == null || mRecyclerView.getAdapter() == null)
            return 0;

        int itemCount = mRecyclerView.getAdapter().getItemCount();

        int eachPageCount = eachPageItemCount();

        if (eachPageCount <= 0) return 0;

        return itemCount % eachPageCount == 0 ?
                itemCount / eachPageCount : itemCount / eachPageCount + 1;
    }
}
